<template>
	<view class="container">
		<view class="form-box">
			<!-- 商品信息 -->
			<view class="goods-section">
				<view class="form-item">
					<text class="label">商品标题</text>
					<input 
						type="text" 
						v-model="formData.title" 
						placeholder="请输入商品标题（15字以内）" 
						:maxlength="15"
					/>
				</view>

				<view class="form-item">
					<text class="label">商品分类</text>
					<picker mode="selector" :range="categories" @change="onCategoryChange">
						<view class="picker">
							<text>{{formData.category || '请选择商品分类'}}</text>
							<uni-icons type="right" size="16" color="#999"></uni-icons>
						</view>
					</picker>
				</view>

				<view class="form-item">
					<text class="label">商品描述</text>
					<textarea 
						v-model="formData.description" 
						placeholder="请详细描述商品的成色、使用年限、出售原因等信息" 
						:maxlength="500"
					/>
					<text class="word-count">{{formData.description.length}}/500</text>
				</view>

				<!-- 商品图片 -->
				<view class="form-item">
					<text class="label">商品图片</text>
					<text class="sub-label">（请上传清晰的实物图，最多6张）</text>
					<view class="image-uploader">
						<view class="image-list" v-if="formData.images.length > 0">
							<view class="image-item" v-for="(image, index) in formData.images" :key="index">
								<image :src="image" mode="aspectFill"></image>
								<text class="delete-btn" @tap="deleteImage(index)">×</text>
							</view>
						</view>
						<view class="upload-btn" @tap="chooseImage" v-if="formData.images.length < 6">
							<uni-icons type="camera" size="32" color="#999"></uni-icons>
							<text>上传图片</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 价格信息 -->
			<view class="price-section">
				<view class="form-item">
					<text class="label">出售价格</text>
					<view class="price-input-box">
						<text class="currency">¥</text>
						<input 
							type="digit" 
							v-model="formData.price" 
							placeholder="请输入价格" 
							class="price-input"
						/>
					</view>
				</view>

				<view class="form-item">
					<text class="label">原价</text>
					<view class="price-input-box">
						<text class="currency">¥</text>
						<input 
							type="digit" 
							v-model="formData.originalPrice" 
							placeholder="请输入原价" 
							class="price-input"
						/>
					</view>
				</view>

				<view class="form-item">
					<text class="label">交易方式</text>
					<radio-group @change="onTradeTypeChange">
						<label class="radio-item" v-for="(item, index) in tradeTypes" :key="index">
							<radio :value="item.value" :checked="formData.tradeType === item.value" color="#2979ff" />
							<text>{{item.label}}</text>
						</label>
					</radio-group>
				</view>
			</view>

			<!-- 交易信息 -->
			<view class="trade-section">
				<view class="form-item">
					<text class="label">交易地点</text>
					<view class="location-select" @tap="selectLocation">
						<text>{{formData.location || '请选择交易地点'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</view>

				<view class="form-item">
					<text class="label">联系方式</text>
					<input 
						type="number" 
						v-model="formData.contact" 
						placeholder="请输入手机号码" 
						maxlength="11"
					/>
				</view>

				<view class="form-item">
					<text class="label">备注信息</text>
					<textarea 
						v-model="formData.remark" 
						placeholder="请输入备注信息（选填）" 
					/>
				</view>
			</view>
		</view>

		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitOrder">
			发布商品
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			categories: ['数码产品', '书籍教材', '生活用品', '服装鞋帽', '体育用品', '其他'],
			tradeTypes: [
				{ label: '自提', value: 'self' },
				{ label: '代送', value: 'delivery' }
			],
			formData: {
				title: '',
				category: '',
				description: '',
				images: [],
				price: '',
				originalPrice: '',
				tradeType: 'self',
				location: '',
				contact: '',
				remark: ''
			}
		}
	},
	methods: {
		onCategoryChange(e) {
			this.formData.category = this.categories[e.detail.value]
		},
		chooseImage() {
			uni.chooseImage({
				count: 1,
				success: (res) => {
					this.formData.images.push(res.tempFilePaths[0])
				}
			})
		},
		deleteImage(index) {
			this.formData.images.splice(index, 1)
		},
		onTradeTypeChange(e) {
			this.formData.tradeType = e.detail.value
		},
		selectLocation() {
			uni.showActionSheet({
				itemList: ['宿舍楼', '教学楼', '图书馆', '食堂', '校门口'],
				success: (res) => {
					this.formData.location = ['宿舍楼', '教学楼', '图书馆', '食堂', '校门口'][res.tapIndex]
				}
			})
		},
		validateForm() {
			if (!this.formData.title) {
				uni.showToast({
					title: '请输入商品标题',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.category) {
				uni.showToast({
					title: '请选择商品分类',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.description) {
				uni.showToast({
					title: '请填写商品描述',
					icon: 'none'
				})
				return false
			}
			if (this.formData.images.length === 0) {
				uni.showToast({
					title: '请上传商品图片',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.price) {
				uni.showToast({
					title: '请输入出售价格',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.location) {
				uni.showToast({
					title: '请选择交易地点',
					icon: 'none'
				})
				return false
			}
			if (!this.formData.contact) {
				uni.showToast({
					title: '请输入联系方式',
					icon: 'none'
				})
				return false
			}
			if (!/^1\d{10}$/.test(this.formData.contact)) {
				uni.showToast({
					title: '请输入正确的手机号',
					icon: 'none'
				})
				return false
			}
			return true
		},
		submitOrder() {
			if (!this.validateForm()) return

			// TODO: 发送请求保存商品信息
			uni.showLoading({
				title: '发布中...'
			})

			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
}

.goods-section, .price-section, .trade-section {
	margin-bottom: 30rpx;
}

.form-item {
	margin-bottom: 30rpx;
	position: relative;

	.label {
		font-size: 28rpx;
		color: $text-color;
		margin-bottom: 20rpx;
		display: block;
	}

	.sub-label {
		font-size: 24rpx;
		color: #999;
		margin-left: 20rpx;
	}

	input, .picker, .location-select {
		width: 100%;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
		color: $text-color;
	}

	.picker, .location-select {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	textarea {
		width: 100%;
		height: 200rpx;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;
		font-size: 28rpx;
	}

	.word-count {
		position: absolute;
		right: 20rpx;
		bottom: 20rpx;
		font-size: 24rpx;
		color: #999;
	}

	.price-input-box {
		display: flex;
		align-items: center;
		background-color: #f5f5f5;
		padding: 20rpx;
		border-radius: 8rpx;

		.currency {
			font-size: 32rpx;
			color: $text-color;
			margin-right: 10rpx;
		}

		.price-input {
			flex: 1;
			background-color: transparent;
			padding: 0;
		}
	}

	.radio-item {
		margin-right: 40rpx;
		font-size: 28rpx;
	}
}

.image-uploader {
	.image-list {
		display: flex;
		flex-wrap: wrap;
		margin-bottom: 20rpx;

		.image-item {
			width: 200rpx;
			height: 200rpx;
			margin-right: 20rpx;
			margin-bottom: 20rpx;
			position: relative;

			image {
				width: 100%;
				height: 100%;
				border-radius: 8rpx;
			}

			.delete-btn {
				position: absolute;
				top: -20rpx;
				right: -20rpx;
				width: 40rpx;
				height: 40rpx;
				background-color: rgba(0, 0, 0, 0.5);
				color: #fff;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 32rpx;
			}
		}
	}

	.upload-btn {
		width: 200rpx;
		height: 200rpx;
		background-color: #f5f5f5;
		border-radius: 8rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		text {
			font-size: 24rpx;
			color: #999;
			margin-top: 10rpx;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 